package com.example.zp.liaotian;

import android.app.AlertDialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.zp.entity.PullMessage;
import com.example.zp.utils.FileUtils;

import java.util.Calendar;
import java.util.List;


public class ChatMsgViewAdapter extends BaseAdapter {

	private  PullMessage pullMessage;

	public static interface IMsgViewType {
		int IMVT_COM_MSG = 0;
		int IMVT_TO_MSG = 1;
	}

	private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();

	private List<ChatMsgEntity> coll;
	private String Top;
	private Context ctx;
	private int to_id,local_id;
	int to_local_id;
	private String system = "0",business = "1",tz;//system:判断是否为系统小秘书，0：否，1：是；business：判断业务布局，1：是否需要维修服务，2：选择地址，3：维修类别，4：选择施工队，5：订单，6：收款申请，7：评价
	private LayoutInflater mInflater;
	private MediaPlayer mMediaPlayer = new MediaPlayer();
	private FileUtils fileUtils;
	private JumpInterface listener;
	private String[] order=new String[4];
	private String[] orderok=new String[4];
	public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll, int to_id, int local_id, JumpInterface listener) {
		this.listener=listener;
		ctx = context;
		this.coll = coll;
		this.to_id = to_id;
		this.local_id = local_id;
		mInflater = LayoutInflater.from(context);
		 fileUtils=new FileUtils(context);
		pullMessage=fileUtils.readObj();
		Log.e("main","数据  "+pullMessage);
		if (to_id == 12) {
			coll.add(new ChatMsgEntity("维修小秘书", "", "您是否需要维修服务?", getDate(), local_id, to_id, false, 0));
		}

	}

	public int getCount() {
		return coll.size();
	}

	public Object getItem(int position) {
		return coll.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
//		ChatMsgEntity entity = coll.get(position);
//
//		if (String.valueOf(local_id).equals(String.valueOf(entity.getTo_id()))) {
//			return IMsgViewType.IMVT_COM_MSG;
//		} else {
//			return IMsgViewType.IMVT_TO_MSG;
//		}
		return coll.get(position).getFlag();


	}

	public int getViewTypeCount() {

		return 10;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

//		final ChatMsgEntity entity = coll.get(position);
//		boolean isComMsg = entity.getMsgType();
//		to_local_id = entity.getTo_id();
	//	Top = entity.getTop();
		ViewHolder viewHolder = null;
		if (convertView == null) {
			switch (getItemViewType(position)){
				case 0:
					convertView = mInflater.inflate(R.layout.secretary_whether, null);
					break;
				case 1:
					convertView = mInflater.inflate(R.layout.secretary_contact_info, null);
					break;
				case 2:
					convertView = mInflater.inflate(R.layout.secretary_maintenance_category, null);
					break;
				case 3:
					convertView = mInflater.inflate(R.layout.secretary_construction_team, null);
					break;
				case 4:
					convertView = mInflater.inflate(R.layout.secretary_order, null);
					break;
				case 5:
					convertView = mInflater.inflate(R.layout.secretary_payment, null);
					break;
				case 6:
					convertView = mInflater.inflate(R.layout.secretary_evaluate, null);
					break;
				case 7:
					if(coll.get(position).isSelf()){
						convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right,null);
					}else{
						convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left,null);
					}
					break;
				case 8:
					break;
				case 9:
					break;
				case 10:
					break;
			}
			viewHolder = new ViewHolder(convertView,getItemViewType(position));


//			//小秘书
//			if (Top.equals("2")) {
//					convertView = mInflater.inflate(R.layout.secretary_whether, null);
//					viewHolder.btn_secretary_need = (Button) convertView.findViewById(R.id.btn_secretary_need);
//					viewHolder.btn_secretary_need.setOnClickListener(new OnClickListener() {
//						public void onClick(View v) {
//							Toast.makeText(ctx,"需要！",Toast.LENGTH_LONG).show();
////							coll.add(new ChatMsgEntity("袋输液","ffdsaf","afdasf",false,3,3,"3"));
//							notifyDataSetChanged();
//						}
//					});
////				} else {
////					convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
////				}
//			//对方
//			}else if(Top.equals("3")) {
//
//			convertView = mInflater.inflate(R.layout.secretary_contact_info, null);
////					viewHolder.btn_secretary_need = (Button) convertView.findViewById(R.id.btn_secretary_need);
////					viewHolder.btn_secretary_need.setOnClickListener(new OnClickListener() {
////						public void onClick(View v) {
////							Toast.makeText(ctx,"需要！",Toast.LENGTH_LONG).show();
////							MySqliteUtlis.GetInstan(ctx).StoringChatData(local_id,"请选择您的联系地址",TestDuiHuaActivity.testMsg.cg_id,getDate(),String.valueOf(TestDuiHuaActivity.testMsg.cg_id),"3");
////						}
////					});
//
//			convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
//
//			//自己
//		}else if(Top.equals("1")){
//
//					convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
//
//					convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
//
//
//			/*else if (system.equals("1")){
//				if (business.equals("1")){
//					if (String.valueOf(local_id).equals(String.valueOf(to_local_id))) {
//						convertView = mInflater.inflate(R.layout.secretary_whether, null);
//						viewHolder.btn_secretary_need = (Button) convertView.findViewById(R.id.btn_secretary_need);
//						viewHolder.btn_secretary_need.setOnClickListener(new OnClickListener() {
//							public void onClick(View v) {
//								Toast.makeText(ctx,"需要！",Toast.LENGTH_LONG).show();
//								chatmsg="2";
//							}
//						});
//					} else {
//						convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
//					}
//
//				}else if (chatmsg.equals("2")){
//					if (String.valueOf(local_id).equals(String.valueOf(to_local_id))) {
//						convertView = mInflater.inflate(R.layout.secretary_contact_info, null);
//
//					}else {
//						convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
//					}
//				}else if (chatmsg.equals("3")){
//					convertView = mInflater.inflate(R.layout.secretary_maintenance_category, null);
//				}else if (chatmsg.equals("4")){
//					convertView = mInflater.inflate(R.layout.secretary_construction_team, null);
//				}else if (chatmsg.equals("5")){
//					convertView = mInflater.inflate(R.layout.secretary_order, null);
//				}else if (chatmsg.equals("6")){
//					convertView = mInflater.inflate(R.layout.secretary_payment, null);
//				}else if (chatmsg.equals("7")){
//					convertView = mInflater.inflate(R.layout.secretary_evaluate, null);
//				}
//			}*/

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		//初始化数据
		initData(viewHolder,position,getItemViewType(position));


//		viewHolder.tvSendTime.setText(entity.getDate());
//
//		if (entity.getText().contains(".amr")) {
//			viewHolder.tvContent.setText("");
//			viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.chatto_voice_playing, 0);
//			viewHolder.tvTime.setText(entity.getTime());
//		} else {
//			viewHolder.tvContent.setText(entity.getText());
//			viewHolder.tvContent.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
//			viewHolder.tvTime.setText("");
//		}
//		viewHolder.tvContent.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				if (entity.getText().contains(".amr")) {
//					playMusic(android.os.Environment.getExternalStorageDirectory()+"/"+entity.getText()) ;
//				}
//			}
//		});
//
//		viewHolder.tvUserName.setText(entity.getName());

		return convertView;
	}

	private void initData(final ViewHolder viewHolder, int position, int type) {

		switch (type) {
			case 0:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				//需要
				viewHolder.btn_secretary_need.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(	new ChatMsgEntity("维修小秘书", "", "请选择您的联系地址", getDate(), local_id, to_id, false, 1));
						viewHolder.btn_secretary_need.setEnabled(false);
					}
				});
				viewHolder.tvContent.setText(coll.get(position).getText());

				break;
			case 1:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				if(order[0]==null){
					viewHolder.tvContactIfo.setText(pullMessage.getCa()[0].gi_text);
				}else{
					viewHolder.tvContactIfo.setText(order[0]);
				}
				if(order[1]==null){
					viewHolder.tvMobile.setText(pullMessage.getCa()[0].mobile);
				}else{
					viewHolder.tvMobile.setText(order[1]);
				}
				viewHolder.btn_contact_modify.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						AlertDialog.Builder builder;
						final AlertDialog alertDialog;
						LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						View layout = inflater.inflate(R.layout.list_contact_info, null);
						final ListView myListView = (ListView) layout.findViewById(R.id.Lv_list_contact_info);
						Contacct_Info_ListAdapter contacct_info_listAdapter = new Contacct_Info_ListAdapter(ctx,pullMessage);
						myListView.setAdapter(contacct_info_listAdapter);
						builder = new AlertDialog.Builder(ctx);
						builder.setView(layout);
						alertDialog = builder.create();
						alertDialog.show();
						myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								View v = myListView.getChildAt(position);
								TextView tv_lcii_ca = (TextView) v.findViewById(R.id.tv_lcii_ca);
								TextView tv_lcii_mobile = (TextView) v.findViewById(R.id.tv_lcii_mobile);
								String st_lcii_ca = tv_lcii_ca.getText().toString();
								String st_lcii_mobile = tv_lcii_mobile.getText().toString();
								order[0]=st_lcii_ca;
								order[1]=st_lcii_mobile;
								alertDialog.cancel();
								notifyDataSetChanged();
							}
						});
					}
				});
				viewHolder.btn_contant_ok.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(new ChatMsgEntity("维修小秘书","","请选择您维修的类别",getDate(),local_id,to_id,false,2));
						viewHolder.btn_contant_ok.setEnabled(false);
						orderok[0] = viewHolder.tvContactIfo.getText().toString();
						orderok[1] = viewHolder.tvMobile.getText().toString();
					}
				});
				break;
			case 2:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				if(order[2]==null){
					viewHolder.tv_mainten_categ.setText(pullMessage.getCatalog()[0]);
				}else{
					viewHolder.tv_mainten_categ.setText(order[2]);
				}
				viewHolder.tv_mainten_categ.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						AlertDialog.Builder builder;
						final AlertDialog alertDialog;
						LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						View layout = inflater.inflate(R.layout.list_maintenance_cetegory, null);
						final ListView myListView = (ListView) layout.findViewById(R.id.Lv_list_maintenance_cetegory);
						Maintenance_Cetegory_ListAdapter maintenance_cetegory_listAdapter = new Maintenance_Cetegory_ListAdapter(ctx,pullMessage);
						myListView.setAdapter(maintenance_cetegory_listAdapter);
						builder = new AlertDialog.Builder(ctx);
						builder.setView(layout);
						alertDialog = builder.create();
						alertDialog.show();
						myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								View v = myListView.getChildAt(position);
								TextView tv_lmci_mc = (TextView) v.findViewById(R.id.tv_lmci_mc);
								String st_lmci_mc = tv_lmci_mc.getText().toString();
								order[2]=st_lmci_mc;
								alertDialog.cancel();
								notifyDataSetChanged();
							}
						});
					}
				});
				viewHolder.btn_mainten_categ_ok.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(new ChatMsgEntity("维修小秘书","","请选择在线的施工队，选择后我们会为您接入您选择的施工队。",getDate(),local_id,to_id,false,3));
						viewHolder.btn_mainten_categ_ok.setEnabled(false);
						orderok[2] = viewHolder.tv_mainten_categ.getText().toString();
					}
				});
				break;
			case 3:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				if(order[3]==null){
					viewHolder.tv_constru_team.setText(pullMessage.getTeam()[0]);
				}else{
					viewHolder.tv_constru_team.setText(order[3]);
				}
				viewHolder.tv_constru_team.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						AlertDialog.Builder builder;
						final AlertDialog alertDialog;
						LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						View layout = inflater.inflate(R.layout.list_construction_team, null);
						final ListView myListView = (ListView) layout.findViewById(R.id.Lv_list_construction_team);
						Construction_Team_ListAdapter construction_team_listAdapter = new Construction_Team_ListAdapter(ctx,pullMessage);
						myListView.setAdapter(construction_team_listAdapter);
						builder = new AlertDialog.Builder(ctx);
						builder.setView(layout);
						alertDialog = builder.create();
						alertDialog.show();
						myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								View v = myListView.getChildAt(position);
								TextView tv_lcti_ct = (TextView) v.findViewById(R.id.tv_lcti_ct);
								String st_lcti_ct = tv_lcti_ct.getText().toString();
								order[3]=st_lcti_ct;
								alertDialog.cancel();
								notifyDataSetChanged();
							}
						});
					}
				});
				viewHolder.btn_constru_ok.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(new ChatMsgEntity("维修小秘书","","您的维修订单已生成，以下是订单的详细信息，在维修队上门维修前，您可以在【我的界面】订单详情中取消该订单",getDate(),local_id,to_id,false,4));
						viewHolder.btn_constru_ok.setEnabled(false);
						orderok[3] = viewHolder.tv_constru_team.getText().toString();
					}
				});
				break;
			case 4:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				viewHolder.tv_order_contact.setText(orderok[0]);
				viewHolder.tv_order_mobile.setText(orderok[1]);
				viewHolder.tv_order_maintenance_category.setText(orderok[2]);
				viewHolder.tv_order_construction_team.setText(orderok[3]);
				viewHolder.btn_order_ok.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(new ChatMsgEntity("维修小秘书","","您收到一份收款申请",getDate(),local_id,to_id,false,5));
						viewHolder.btn_order_ok.setEnabled(false);
					}
				});
				break;
			case 5:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				viewHolder.btn_payment.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						notifyData(new ChatMsgEntity("维修小秘书","","支付成功，请您对施工队的服务进行评价",getDate(),local_id,to_id,false,6));
						viewHolder.btn_payment.setEnabled(false);
					}
				});
				break;
			case 6:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				viewHolder.btn_evaluate.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {

					}
				});
				break;
			case 7:
				viewHolder.tvUserName.setText(coll.get(position).getName());
				viewHolder.tvSendTime.setText(getDate());
				viewHolder.tvContent.setText(coll.get(position).getText());
				break;
			case 8:
				break;
			case 9:
				break;
			case 10:
				break;

		}

	}
	public void notifyData(ChatMsgEntity data){
		coll.add(data);
		notifyDataSetChanged();
		listener.scroll();

	}
	private final 	static class ViewHolder {
		public TextView tvSendTime;
		public TextView tvUserName;
		public TextView tvContent;
		public TextView tvTime;
		public TextView btn_secretary_need;
		public TextView btn_secretary_unwanted;
		public TextView tvContactIfo;
		public TextView tvMobile;
		public TextView btn_contant_ok;
		public TextView btn_contact_modify;
		public TextView tv_mainten_categ;
		public TextView btn_mainten_categ_ok;
		public TextView btn_constru_ok;
		public TextView tv_constru_team;
		public TextView tv_order_contact;
		public TextView tv_order_mobile;
		public TextView tv_order_maintenance_category;
		public TextView tv_order_construction_team;
		public TextView btn_order_ok;
		public ListView Lv_payment;
		public TextView btn_payment;
		public TextView btn_evaluate;


	/**
	 * 先从小秘书开始   7种 0-6   人7-后
	 * @param convertView
	 * @param type
     */
		public ViewHolder(View convertView, int type){
			tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
			tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			switch (type){
				case 0:
					btn_secretary_need = (TextView) convertView.findViewById(R.id.btn_secretary_need);
					btn_secretary_unwanted = (TextView) convertView.findViewById(R.id.btn_secretary_unwanted);
					break;
				case 1:
					tvContactIfo = (TextView) convertView.findViewById(R.id.tv_contact_contact_info);
					tvMobile = (TextView) convertView.findViewById(R.id.tv_contact_mobile);
					btn_contant_ok = (TextView) convertView.findViewById(R.id.btn_contant_ok);
					btn_contact_modify = (TextView) convertView.findViewById(R.id.btn_contact_modify);
					break;
				case 2:
					tv_mainten_categ = (TextView) convertView.findViewById(R.id.tv_mainten_categ);
					btn_mainten_categ_ok = (TextView) convertView.findViewById(R.id.btn_mainten_categ_ok);
					break;
				case 3:
					tv_constru_team = (TextView) convertView.findViewById(R.id.tv_constru_team);
					btn_constru_ok = (TextView) convertView.findViewById(R.id.btn_constru_ok);
					break;
				case 4:
					tv_order_contact = (TextView) convertView.findViewById(R.id.tv_order_contact);
					tv_order_mobile = (TextView) convertView.findViewById(R.id.tv_order_mobile);
					tv_order_maintenance_category = (TextView) convertView.findViewById(R.id.tv_order_maintenance_category);
					tv_order_construction_team = (TextView) convertView.findViewById(R.id.tv_order_construction_team);
					btn_order_ok = (TextView) convertView.findViewById(R.id.btn_order_ok);
					break;
				case 5:
					Lv_payment = (ListView) convertView.findViewById(R.id.Lv_payment);
					btn_payment = (TextView) convertView.findViewById(R.id.btn_payment);
					break;
				case 6:
					btn_evaluate = (TextView) convertView.findViewById(R.id.btn_evaluate);
					break;
				case 7:

					break;
				case 8:
					break;
				case 9:
					break;
				case 10:
					break;

			}


		}

	}

	/**
	 * @Description
	 * @param name
	 */
	private void playMusic(String name) {
		try {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			}
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(name);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void stop() {

	}
	private String getDate() {
		Calendar c = Calendar.getInstance();

		String year = String.valueOf(c.get(Calendar.YEAR));
		String month = String.valueOf(c.get(Calendar.MONTH));
		String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
		String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
		String mins = String.valueOf(c.get(Calendar.MINUTE));

		StringBuffer sbBuffer = new StringBuffer();
		sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":"
				+ mins);

		return sbBuffer.toString();
	}
	public interface JumpInterface{
		void scroll();
	}
}
